<HTML>
 
<!-- Mirrored from www.javapractices.com/apps/movies/javadoc/src-html/hirondelle/movies/edit/MovieController.html by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:30:20 GMT -->
<HEAD>
  <TITLE>
MovieController.java
  </TITLE>
  <LINK REL ='stylesheet' TYPE='text/css' HREF='../../../../highlight.css' TITLE='Style'>
 </HEAD>
 <BODY>
<PRE>
<span class='keyword'>package</span> hirondelle.movies.edit;<a name=line.1></a>
<a name=line.2></a>
<span class='keyword'>import</span> hirondelle.movies.exception.InvalidInputException;<a name=line.3></a>
<span class='keyword'>import</span> hirondelle.movies.main.MainWindow;<a name=line.4></a>
<span class='keyword'>import</span> hirondelle.movies.util.Edit;<a name=line.5></a>
<span class='keyword'>import</span> hirondelle.movies.util.Util;<a name=line.6></a>
<a name=line.7></a>
<span class='keyword'>import</span> java.awt.event.ActionEvent;<a name=line.8></a>
<span class='keyword'>import</span> java.awt.event.ActionListener;<a name=line.9></a>
<span class='keyword'>import</span> java.util.logging.Logger;<a name=line.10></a>
<a name=line.11></a>
<span class='keyword'>import</span> javax.swing.JOptionPane;<a name=line.12></a>
<a name=line.13></a>
<span class='comment'>/**<a name=line.14></a>
 Add a new {@link Movie} to the database, or change an existing one. <a name=line.15></a>
 <a name=line.16></a>
 &lt;P&gt;It's important to note that this class uses most of the other classes in this feature  <a name=line.17></a>
 to get its job done (it doesn't use the &lt;tt&gt;Action&lt;/tt&gt; classes):<a name=line.18></a>
 &lt;ul&gt;<a name=line.19></a>
   &lt;li&gt;it gets user input from the view - {@link MovieView}<a name=line.20></a>
   &lt;li&gt;it validates user input using the model - {@link Movie}<a name=line.21></a>
   &lt;li&gt;it persists the data using the Data Access Object  - {@link MovieDAO}<a name=line.22></a>
 &lt;/ul&gt;<a name=line.23></a>
*/</span><a name=line.24></a>
<span class='keyword'>final</span> <span class='keyword'>class</span> MovieController <span class='keyword'>implements</span> ActionListener {<a name=line.25></a>
  <a name=line.26></a>
  <span class='comment'>/**<a name=line.27></a>
   Constructor.<a name=line.28></a>
   @param aView user interface<a name=line.29></a>
   @param aEdit identifies what type of edit - add or change <a name=line.30></a>
  */</span><a name=line.31></a>
  MovieController(MovieView aView, Edit aEdit){<a name=line.32></a>
    fView = aView;<a name=line.33></a>
    fEdit = aEdit;<a name=line.34></a>
  }<a name=line.35></a>
  <a name=line.36></a>
  <span class='comment'>/**<a name=line.37></a>
   Attempt to add a new {@link Movie}, or edit an existing one.<a name=line.38></a>
   <a name=line.39></a>
   &lt;P&gt;If the input is invalid, then inform the user of the problem(s).<a name=line.40></a>
   If the input is valid, then add or change the &lt;tt&gt;Movie&lt;/tt&gt;, close the dialog, <a name=line.41></a>
   and update the main window's display.  <a name=line.42></a>
  */</span><a name=line.43></a>
  <span class='keyword'>public</span> <span class='keyword'>void</span> actionPerformed(ActionEvent aEvent){<a name=line.44></a>
    fLogger.fine(<span class='literal'>"Editing movie"</span> + fView.getTitle());<a name=line.45></a>
    <span class='keyword'>try</span> {<a name=line.46></a>
      createValidMovieFromUserInput();<a name=line.47></a>
    }<a name=line.48></a>
    <span class='keyword'>catch</span>(InvalidInputException ex){<a name=line.49></a>
      informUserOfProblems(ex);<a name=line.50></a>
    }<a name=line.51></a>
    <span class='keyword'>if</span> ( isUserInputValid() ){<a name=line.52></a>
      <span class='keyword'>if</span>( Edit.ADD == fEdit ) {<a name=line.53></a>
        fLogger.fine(<span class='literal'>"Add operation."</span>);<a name=line.54></a>
        fDAO.add(fMovie);<a name=line.55></a>
      }<a name=line.56></a>
      <span class='keyword'>else</span> <span class='keyword'>if</span> (Edit.CHANGE == fEdit) {<a name=line.57></a>
        fLogger.fine(<span class='literal'>"Change operation."</span>);<a name=line.58></a>
        fDAO.change(fMovie);<a name=line.59></a>
      }<a name=line.60></a>
      <span class='keyword'>else</span> {<a name=line.61></a>
        <span class='keyword'>throw</span> <span class='keyword'>new</span> AssertionError();<a name=line.62></a>
      }<a name=line.63></a>
      fView.closeDialog();<a name=line.64></a>
      MainWindow.getInstance().refreshView();<a name=line.65></a>
    }<a name=line.66></a>
  }<a name=line.67></a>
<a name=line.68></a>
  <span class='comment'>// PRIVATE //<a name=line.69></a>
</span>  <span class='keyword'>private</span> <span class='keyword'>final</span> MovieView fView;<a name=line.70></a>
  <span class='keyword'>private</span> Movie fMovie;<a name=line.71></a>
  <span class='keyword'>private</span> Edit fEdit;<a name=line.72></a>
  <span class='keyword'>private</span> MovieDAO fDAO = <span class='keyword'>new</span> MovieDAO();<a name=line.73></a>
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>final</span> Logger fLogger = Util.getLogger(MovieController.<span class='keyword'>class</span>);<a name=line.74></a>
  <a name=line.75></a>
  <span class='keyword'>private</span> <span class='keyword'>void</span> createValidMovieFromUserInput() <span class='keyword'>throws</span> InvalidInputException {<a name=line.76></a>
    fMovie = <span class='keyword'>new</span> Movie(<a name=line.77></a>
      fView.getId(), fView.getTitle(), fView.getDateViewed(), <a name=line.78></a>
      fView.getRating(), fView.getComment()<a name=line.79></a>
    );<a name=line.80></a>
  }<a name=line.81></a>
<a name=line.82></a>
  <span class='keyword'>private</span> <span class='keyword'>boolean</span> isUserInputValid(){<a name=line.83></a>
    <span class='keyword'>return</span> fMovie != <span class='keyword'>null</span>;<a name=line.84></a>
  }<a name=line.85></a>
  <a name=line.86></a>
  <span class='keyword'>private</span> <span class='keyword'>void</span> informUserOfProblems(InvalidInputException aException) {<a name=line.87></a>
    Object[] messages = aException.getErrorMessages().toArray();<a name=line.88></a>
    JOptionPane.showMessageDialog(<a name=line.89></a>
      fView.getDialog(), messages, <span class='literal'>"Movie cannot be added"</span>, JOptionPane.ERROR_MESSAGE<a name=line.90></a>
    );<a name=line.91></a>
  }<a name=line.92></a>
} <a name=line.93></a>
</PRE><a name=line.94></a>
 </BODY><a name=line.95></a>
<HTML><a name=line.96></a>
